package com.younio.local.driver;

import com.younio.local.service.Toast;
import lombok.extern.slf4j.Slf4j;
import org.apache.derby.jdbc.ClientDriver;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;

@Slf4j
public class DerbyDriver extends ClientDriver implements Driver {

    @Override
    public Connection connect(String s, Properties properties) {

        int j = 1;
        while (true) {
            try {
                return super.connect(s, properties);
            } catch (SQLException e) {
                if (j % 100 == 0) {
                    Toast.error("连接天马数据库失败,已尝试重连" + j + "次，，正在重试下一次重连", e.getMessage());
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                    log.info("连接天马数据库失败,第{}次", j);
                }
                j++;
                if (j > 500) {
                    Toast.error("连接天马数据库失败，失败次数" + j + "，程序即将退出", e.getMessage());
                    log.info("连接天马数据库失败,{}", e.getMessage());
                    log.info("系统退出");

                    System.exit(-1);
                }
            }
        }

    }
}
